<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>银行系统 - 我的贷款</title>
<style>
body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
}

.header {
    background-color: #333;
    color: white;
    padding: 20px;
    text-align: center;
}

.navbar {
    background-color: #444;
    overflow: hidden;
}

.navbar a {
    float: left;
    display: block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

.navbar a:hover {
    background-color: #555;
}

.navbar a.active {
    background-color: #555;
}

.content {
    padding: 20px;
    max-width: 1200px;
    margin: 0 auto;
}

.loan-list {
    margin-top: 30px;
}

.loan-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 20px;
}

.loan-table th, .loan-table td {
    border: 1px solid #ddd;
    padding: 12px 15px;
    text-align: left;
}

.loan-table th {
    background-color: #f2f2f2;
    font-weight: bold;
}

.loan-table tr:nth-child(even) {
    background-color: #f9f9f9;
}

.loan-table tr:hover {
    background-color: #f5f5f5;
}

.status-pending { color: #FF9800; }
.status-approved { color: #4CAF50; }
.status-rejected { color: #F44336; }
.status-paid { color: #2196F3; }

.repayment-form {
    margin-top: 20px;
    padding: 20px;
    background-color: #f1f1f1;
    border-radius: 5px;
    display: none;
    max-width: 500px;
}

.form-group {
    margin-bottom: 15px;
}

.form-group label {
    display: block;
    margin-bottom: 5px;
    font-weight: bold;
}

.form-group input {
    width: 100%;
    padding: 8px;
    border: 1px solid #ddd;
    border-radius: 4px;
    box-sizing: border-box;
}

button {
    background-color: #4CAF50;
    color: white;
    padding: 10px 15px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

button:hover {
    background-color: #45a049;
}

.error-message {
    color: #F44336;
    margin: 10px 0;
    padding: 10px;
    background-color: #ffcccc;
    border-radius: 4px;
}

.success-message {
    color: #4CAF50;
    margin: 10px 0;
    padding: 10px;
    background-color: #ccffcc;
    border-radius: 4px;
}

.no-loans {
    margin: 50px 0;
    text-align: center;
    padding: 20px;
    background-color: #f9f9f9;
    border-radius: 5px;
}

.no-loans a {
    color: #4CAF50;
    text-decoration: none;
}

.no-loans a:hover {
    text-decoration: underline;
}
</style>
</head>
<body>
    <div class="header">
        <h1>银行系统 - 我的贷款</h1>
        <p>欢迎您，<c:out value="${sessionScope.user.username}" /></p>
    </div>
    
    <div class="navbar">
        <a href="index.jsp">首页</a>
        <a href="loan" class="active">我的贷款</a>
        <a href="applyLoan">申请贷款</a>
        <a href="account">我的账户</a>
        <a href="logout">退出登录</a>
    </div>
    
    <div class="content">
        <h2>我的贷款管理</h2>
        
        <c:if test="${not empty error}">
            <div class="error-message">${error}</div>
        </c:if>
        
        <c:if test="${not empty success}">
            <div class="success-message">${success}</div>
        </c:if>
        
        <c:if test="${empty loans}">
            <div class="no-loans">
                <p>您还没有贷款记录</p>
                <a href="applyLoan">立即申请贷款</a>
            </div>
        </c:if>
        
        <c:if test="${not empty loans}">
            <div class="loan-list">
                <table class="loan-table">
                    <tr>
                        <th>贷款编号</th>
                        <th>申请金额</th>
                        <th>剩余金额</th>
                        <th>申请时间</th>
                        <th>到期日</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                    <c:forEach items="${loans}" var="loan">
                        <tr>
                            <td>${loan.loan_number}</td>
                            <td>¥${loan.amount}</td>
                            <td>¥${loan.remaining_amount}</td>
                            <td>${loan.create_time}</td>
                            <td>${loan.due_date}</td>
                            <td>
                                <span class="${loan.statusClass}">${loan.statusDesc}</span>
                            </td>
                            <td>
                                <c:if test="${loan.status == 2}">
                                    <button onclick="showRepaymentForm(${loan.id}, ${loan.remaining_amount})">还款</button>
                                </c:if>
                                <c:if test="${loan.status != 2}">
                                    <span>${loan.statusDesc}</span>
                                </c:if>
                            </td>
                        </tr>
                    </c:forEach>
                </table>
            </div>
        </c:if>
    </div>
    
    <!-- 还款表单 -->
    <div id="repaymentForm" class="repayment-form">
        <h3>偿还贷款</h3>
        <form action="repayLoan" method="post">
            <input type="hidden" id="loanId" name="loanId">
            <div class="form-group">
                <label for="repayAmount">还款金额 (元)</label>
                <input type="number" id="repayAmount" name="amount" step="0.01" required>
                <small id="amountHint" style="display: block; margin-top: 5px; color: #666;">
                    最大可还款金额: <span id="maxRepayAmount">--</span> 元
                </small>
            </div>
            <div class="form-group">
                <label for="repayDesc">还款说明 (可选)</label>
                <input type="text" id="repayDesc" name="description" placeholder="输入还款说明...">
            </div>
            <div class="form-group" style="display: flex; gap: 10px;">
                <button type="submit">确认还款</button>
                <button type="button" onclick="hideRepaymentForm()">取消</button>
            </div>
        </form>
    </div>

    <script>
        function showRepaymentForm(loanId, maxAmount) {
            document.getElementById('loanId').value = loanId;
            document.getElementById('maxRepayAmount').textContent = maxAmount.toFixed(2);
            document.getElementById('repaymentForm').style.display = 'block';
            
            // 自动填充最大可还款金额
            document.getElementById('repayAmount').value = maxAmount.toFixed(2);
            
            // 限制输入金额不超过最大可还款金额
            document.getElementById('repayAmount').addEventListener('input', function() {
                if (parseFloat(this.value) > maxAmount) {
                    this.value = maxAmount.toFixed(2);
                    alert('还款金额不能超过最大可还款金额');
                }
            });
        }

        function hideRepaymentForm() {
            document.getElementById('repaymentForm').style.display = 'none';
        }
    </script>
</body>
</html>